from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from model import Teacher, Course

# 1. 链接数据库等准备工作
engine = create_engine('mysql+pymysql://root:python@localhost:3306/19python')
DBSession = sessionmaker(bind=engine)
session = DBSession()

# 2. 查询数据
# 查询id为5的老师
teacher = session.query(Teacher).filter(Teacher.id == 1).one()
print(teacher.name)  # 老师名字
print(teacher.course.name)  # 老师教的课程的名字

print('--------------------------------------')
# 查询id为3的课程
course = session.query(Course).filter(Course.id == 3).one()
print(course.name)  # 课程的名字
print(course.teacher.name)  # 课程对应的老师的名字

# 4. 关闭session
session.close()
